package top.metaspace.singleton.serialization;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import top.metaspace.singleton.LazySingleton;

/**
 * @author panjun
 * @date 2018/6/21. 序列化破坏单例
 */
public class SerializationSingletonTest {

	public static void main(String[] args) {
		ObjectOutputStream oos = null;
		ObjectInputStream ois = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream("tempFile"));
			oos.writeObject(LazySingleton.getInstance());
			File file = new File("tempFile");
			ois = new ObjectInputStream(new FileInputStream(file));
			LazySingleton singleton = (LazySingleton)ois.readObject();

			System.out.println(singleton==LazySingleton.getInstance());
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				if (ois!=null){
					ois.close();
				}
				if (oos!=null){
					oos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}


	}

}
